﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.Common.ValueObject;

namespace ERP_RORZE_TOOL.DBAccess
{
	class BrokenDAO : BaseDAO
	{
		#region Basic Method

		public static DataTable GetAll()
		{
			return ExcuteProcedure(ToolConstants.StoreProcedure.Broken.GetAll);
		}

		public static DataTable GetOne(int brokenID)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@ID", SqlDbType.Int, brokenID)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Broken.GetOne, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

        public static DataTable GetAll(int range)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
						CreateParameter("@Range", SqlDbType.Int, range)
                    };
				return ExcuteProcedure(ToolConstants.StoreProcedure.Broken.GetAllWithDateRange, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static Broken LoadOne(int brokenID)
		{
			Broken o = new Broken();
			DataTable dt = GetOne(brokenID);
			if(dt.Rows.Count > 0)
			{
				o.ID = int.Parse(dt.Rows[0][ToolConstants.Entities.Broken.FieldName.ID].ToString());
				o.Date = DateTime.Parse(dt.Rows[0][ToolConstants.Entities.Broken.FieldName.Date].ToString());
				o.ToolID = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.ToolID].ToString();
				o.MachineID = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.MachineID].ToString();
				o.MistakeID = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.MistakeID].ToString();
                o.StatusName = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.StatusName].ToString();
				o.LossNo = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.LossNo].ToString();
                o.PartNo = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.PartNo].ToString();
				o.Quantity = !Utilities.IsNullOrEmpty(dt.Rows[0][ToolConstants.Entities.Broken.FieldName.Quantity].ToString()) ? int.Parse(dt.Rows[0][ToolConstants.Entities.Broken.FieldName.Quantity].ToString()) : 0;
				o.ProcessStatusID = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.ProcessStatusID].ToString();
				o.RotateID = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.RotateID].ToString();
				o.StandardSpeed = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.StandardSpeed].ToString();
				o.RealitySpeed = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.RealitySpeed].ToString();
				o.Process = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.Process].ToString();
				o.ToKind = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.ToKind].ToString();
				o.Note = dt.Rows[0][ToolConstants.Entities.Broken.FieldName.Note].ToString();

			}

			return o;
		}


		public static int Insert(Broken o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@MachineID", SqlDbType.NVarChar, o.MachineID),
                        CreateParameter("@MistakeID", SqlDbType.NVarChar, o.MistakeID),
                        CreateParameter("@StatusName", SqlDbType.NVarChar, o.StatusName),
                        CreateParameter("@LossNo", SqlDbType.NVarChar, o.LossNo),
                        CreateParameter("@PartNo", SqlDbType.NVarChar, o.PartNo),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@ProcessStatusID", SqlDbType.NVarChar, o.ProcessStatusID),
                        CreateParameter("@RotateID", SqlDbType.NVarChar, o.RotateID),
                        CreateParameter("@StandardSpeed", SqlDbType.NVarChar, o.StandardSpeed),
                        CreateParameter("@RealitySpeed", SqlDbType.NVarChar, o.RealitySpeed),
                        CreateParameter("@Process", SqlDbType.NVarChar, o.Process),
                        CreateParameter("@ToKind", SqlDbType.NVarChar, o.ToKind),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Broken.Insert, parameters);

			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}

		}

		public static int Update(Broken o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID),
                        CreateParameter("@Date", SqlDbType.DateTime, o.Date),
                        CreateParameter("@ToolID", SqlDbType.NVarChar, o.ToolID),
                        CreateParameter("@MachineID", SqlDbType.NVarChar, o.MachineID),
                        CreateParameter("@MistakeID", SqlDbType.NVarChar, o.MistakeID),
                        CreateParameter("@StatusName", SqlDbType.NVarChar, o.StatusName),
                        CreateParameter("@LossNo", SqlDbType.NVarChar, o.LossNo),
                        CreateParameter("@PartNo", SqlDbType.NVarChar, o.PartNo),
                        CreateParameter("@Quantity", SqlDbType.Int, o.Quantity),
                        CreateParameter("@ProcessStatusID", SqlDbType.NVarChar, o.ProcessStatusID),
                        CreateParameter("@RotateID", SqlDbType.NVarChar, o.RotateID),
                        CreateParameter("@StandardSpeed", SqlDbType.NVarChar, o.StandardSpeed),
                        CreateParameter("@RealitySpeed", SqlDbType.NVarChar, o.RealitySpeed),
                        CreateParameter("@Process", SqlDbType.NVarChar, o.Process),
                        CreateParameter("@ToKind", SqlDbType.NVarChar, o.ToKind),
                        CreateParameter("@Note", SqlDbType.NVarChar, o.Note),
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Broken.Update, parameters);
			}
			catch(Exception e)
			{
				MessageBox.Show(e.ToString(), ToolConstants.Form.MessageBox.Error, MessageBoxButtons.OK);
				return 0;
				//throw new BaseDAO.MyException(e);
			}
		}

		public static int Delete(Broken o)
		{
			try
			{
				var parameters = new SqlParameter[]
                    {
                        CreateParameter("@ID", SqlDbType.Int, o.ID)
                    };

				return ExecuteNonQueryProcedure(ToolConstants.StoreProcedure.Broken.Delete, parameters);
			}
			catch(Exception e)
			{
				throw new BaseDAO.MyException(e);
			}
		}

		public static DataTable GetTemplateTable()
		{
			DataTable table = new DataTable("333_Broken");

			return table;
		}

		#endregion
	}
}
